<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" onclick="openeditModal()" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="deleteTemplateByIds()" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  >									
									<button class="btn btn-default">查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="template">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        

                        </div>
                        <!-- 数据表格 /-->

                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td>
						<input type="hidden" id="templateId" >
						<input  class="form-control" id="name" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brand" multiple="multiple" class="form-control select2"></select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td >
						<select id="spec" multiple="multiple" class="form-control select2">

						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" onclick="addTable()" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="table">

							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" onclick="save()" aria-hidden="true">保存</button>
			<button class="btn btn-danger" data-dismiss="modal" onclick="update()" aria-hidden="true">修改</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
    
</body>

</html>
<script>
	$(function () {
		getListTemplate();
		$("#spec").select2({
			language : "zh-CN",
			minimumInputLength : 0,
			allowClear: true,
		})
		$("#brand").select2({
			language : "zh-CN",
			minimumInputLength : 0,
			allowClear: true,
		})
	})
	function getListTemplate() {
		$.ajax({
			url : "http://localhost:9001/template/listTemplate",
			dataType : "json",
			type : "get",
			success : function (data) {
				if (data.code == 200) {
					var tr = "";
					var list = data.data;
					console.log(list)
					for (var i = 0; i < list.length; i++) {
						tr += "<tr>";
						tr += "<td><input  type='checkbox' value='"+list[i].id+"'></td>"
						tr += "<td>"+list[i].id+"</td>"
						tr += "<td>"+list[i].name+"</td>"
						var spec = JSON.parse(list[i].specIds);
						var specText = "";
						for (var j = 0; j < spec.length; j++) {
							if (j == spec.length -1) {
								specText += spec[j].text;
								continue;
							}
							specText += spec[j].text + ",";
						}
						tr += "<td>"+specText+"</td>"
						var brand = JSON.parse(list[i].brandIds);
						var brandText = "";
						for (var j = 0; j < brand.length; j++) {
							if (j == brand.length -1) {
								brandText += brand[j].text;
								continue;
							}
							brandText += brand[j].text + ",";
						}
						tr += "<td>"+brandText+"</td>"
						var custom = JSON.parse(list[i].customAttributeItems);
						var customText = "";
						for (var j = 0; j < custom.length; j++) {
							if (j == custom.length -1) {

								customText += custom[j].text;
								continue;
							}
							customText += custom[j].text + ",";
						}
						tr += "<td>"+customText+"</td>"
						tr += "<td><button type='button' class='btn bg-olive btn-xs' onclick='openUpdate("+list[i].id+")'>修改</button></td>"
						tr += "</tr>";
					}
					$("#template").html(tr);
				} else {
					alert(data.message)
				}
			},
			error : function (e) {
				console.log(e)
			}
		})
	}

	// 动态添加表格
	function addTable() {
		var tr = "";
		tr += "<tr>"
		tr += '<td><input type="checkbox" class="icheckbox_square-blue" ></td>'
		tr += "<td><input type='text' class='form-control' placeholder='属性名称' /></td>"
		tr += '<td><button type="button" class="btn btn-default" onclick="deleteTable(this);" title="删除">删除</button> </td>'
		tr += "</tr>"
		$("#table").append(tr)
	}
	// 移除当前td
	function deleteTable(nowTr) {
		$(nowTr).parent().parent().remove();

	}

	function openeditModal() {
		$("#editModal").modal("show");
		var brandList;
		$.ajax({
			url : "http://localhost:9001/brand/listBrand/",
			dataType : "json",
			type : "post",
			async: false,
			success : function (data) {
				if (data.code == 200) {
					brandList = data.data;
				} else {
					alert(data.message)
				}
			},
			error : function (e) {
				alert("系统错误");
				console.log(e)
			}
		})
		var options = "";
		for (var i = 0; i < brandList.length; i++) {
			options += "<option value='"+brandList[i].id+"'>"+brandList[i].name+"</option>"
		}
		$("#brand").html(options)

		var specList;
		$.ajax({
			url : "http://localhost:9001/specification/listSpecification/",
			dataType : "json",
			type : "get",
			async: false,
			success : function (data) {
				specList = data.data;
			},
			error : function (e) {
				console.profile(e);
				alert("系统错误")
			}
		})
		console.log(specList)
		var specOptions = "";
		for (var i = 0; i < specList.length; i++) {
			specOptions += "<option value='"+specList[i].id+"'>"+specList[i].specName+"</option>"
		}
		$("#spec").html(specOptions)
	}

	function save() {
		// 获取select2 brand里的数据
		var brandData = $("#brand").select2("data");
		console.log(brandData)
		var brandList = [];
		$.each(brandData,function (i,e) {
			var brand = {id:e.id,text:e.text}
			brandList.push(brand);
		})
		// 装成json字符串
		var brandListJson = JSON.stringify(brandList)

		// 获取selec2 spec里的数据
		var specData = $("#spec").select2("data");
		var specList = [];
		$.each(specData,function (i,e) {
			var spec = {id:e.id,text:e.text}
			specList.push(spec)
		})

		var specListJson = JSON.stringify(specList);

		// 获取添加的表格中的数据
		var optionList = [];
		var trs = $("#table").contents().find("tr").prevObject;
		trs.filter("tr");
		for (var i = 0; i < trs.length; i++) {
			var tds = $(trs[i]).find("td");
			var order = $(tds[1]).find("input").val();
			if (order != null && order != undefined && order != "") {
				var options = {"text":order};
				optionList.push(options);
			}
		}
		var specOption = JSON.stringify(optionList);
		console.log(specOption)
		$.ajax({
			url : "http://localhost:9001/template/addTemplate",
			dataType : "json",
			data : {
				name : $("#name").val(),
				brandIds : brandListJson,
				specIds : specListJson,
				customAttributeItems : specOption
			},
			type : "post",
			success : function (data) {
				alert(data.message)
				getListTemplate();
			}
		})
	}

	function openUpdate(id) {
		$("#editModal").modal("show");
		var template;
		$.ajax({
			url : "http://localhost:9001/template/templateById/"+id,
			dataType : "json",
			type : "get",
			async: false,
			success : function (data) {
				if (data.code == 200) {
					template = data.data;
				} else {
					alert(data.message)
				}
			}
		})
		console.log(template)
		$("#templateId").val(template.id);
		$("#name").val(template.name);
		var brandTemplate = JSON.parse(template.brandIds);
		var specTemplate = JSON.parse(template.specIds);
		var customAttributeItemsTemplate = JSON.parse(template.customAttributeItems)

		var brandList;
		$.ajax({
			url : "http://localhost:9001/brand/listBrand/",
			dataType : "json",
			type : "post",
			async: false,
			success : function (data) {
				if (data.code == 200) {
					brandList = data.data;
				} else {
					alert(data.message)
				}
			},
			error : function (e) {
				alert("系统错误");
				console.log(e)
			}
		})

		var options = "";
		for (var i = 0; i < brandList.length; i++) {
				options += "<option value='"+brandList[i].id+"'>"+brandList[i].name+"</option>"
		}
		$("#brand").html(options)


		$.each(brandTemplate,function (i,e) {
			$("#brand").append(new Option(e.text,e.id,false,true));
		})
		$("#brand").trigger("change")
		var specList;
		$.ajax({
			url : "http://localhost:9001/specification/listSpecification/",
			dataType : "json",
			type : "get",
			async: false,
			success : function (data) {
				specList = data.data;
			},
			error : function (e) {
				console.profile(e);
				alert("系统错误")
			}
		})
		console.log(specList)
		var specOptions = "";
		for (var i = 0; i < specList.length; i++) {
			specOptions += "<option value='"+specList[i].id+"'>"+specList[i].specName+"</option>"
		}
		$("#spec").html(specOptions)
		$.each(specTemplate,function (i,e) {
			$("#spec").append(new Option(e.text,e.id,false,true));
		})
		$("#spec").trigger("change")
		// 添加表格数据
		var tr = "";
		for (var i = 0; i < customAttributeItemsTemplate.length; i++) {
			tr += "<tr>"
			tr += "<td><input type='checkbox' class='icheckbox_square-blue' ></td>"
			tr += "<td><input type='text' class='form-control' placeholder='属性名称' value='"+customAttributeItemsTemplate[i].text+"' /></td>"
			tr += '<td><button type="button" class="btn btn-default" onclick="deleteTable(this);" title="删除">删除</button> </td>'
			tr += "</tr>"
		}

		$("#table").html(tr)
	}
	function update() {
		// 获取select2 brand里的数据
		var brandData = $("#brand").select2("data");
		console.log(brandData)
		var brandList = [];
		$.each(brandData,function (i,e) {
			var brand = {id:e.id,text:e.text}
			brandList.push(brand);
		})
		// 装成json字符串
		var brandListJson = JSON.stringify(brandList)

		// 获取selec2 spec里的数据
		var specData = $("#spec").select2("data");
		var specList = [];
		$.each(specData,function (i,e) {
			var spec = {id:e.id,text:e.text}
			specList.push(spec)
		})

		var specListJson = JSON.stringify(specList);

		// 获取添加的表格中的数据
		var optionList = [];
		var trs = $("#table").contents().find("tr").prevObject;
		trs.filter("tr");
		for (var i = 0; i < trs.length; i++) {
			var tds = $(trs[i]).find("td");
			var order = $(tds[1]).find("input").val();
			if (order != null && order != undefined && order != "") {
				var options = {"text":order};
				optionList.push(options);
			}
		}
		var specOption = JSON.stringify(optionList);
		$.ajax({
			url : "http://localhost:9001/template/updateTemplate",
			dataType : "json",
			data : {
				id : $("#templateId").val(),
				name : $("#name").val(),
				brandIds : brandListJson,
				specIds : specListJson,
				customAttributeItems : specOption
			},
			type : "post",
			success : function (data) {
				alert(data.message)
				getListTemplate();
			}
		})
	}

	function deleteTemplateByIds() {
		var arr = [];
		$('input[type="checkbox"]:checked').each(function (i,e) {
			arr.push($(this).val())
		})
		$.ajax({
			url : "http://localhost:9001/template/deleteTemplateByIds",
			dataType : "json",
			type : "post",
			data :{
				ids : arr
			},
			success : function (data) {
				alert(data.message);
				getListBrand()
			}
		})
	}
</script>